<?php
// 准备要加密的 XML 订单信息
$xmlData = '<?xml version="1.0" encoding="UTF-8"?>
<Order>
    <OrderHead>
        <OrderID>12345</OrderID>
        <CustomerName>John Doe</CustomerName>
        <OrderDate>2024-04-19</OrderDate>
        <ShippingAddress>
            <StreetAddress>123 Main St</StreetAddress>
            <City>New York</City>
            <State>NY</State>
            <PostalCode>10001</PostalCode>
            <Country>USA</Country>
        </ShippingAddress>
        <Items>
            <Item>
                <ProductID>67890</ProductID>
                <ProductName>Laptop</ProductName>
                <Quantity>2</Quantity>
                <UnitPrice>800.00</UnitPrice>
            </Item>
            <Item>
                <ProductID>54321</ProductID>
                <ProductName>Smartphone</ProductName>
                <Quantity>1</Quantity>
                <UnitPrice>600.00</UnitPrice>
            </Item>
        </Items>
    </OrderHead>
</Order>';

// 提供的 RSA 公钥
$rsaPublicKey = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDQSQrReErVFRmFnHKl8U5XkrkHWr87Qp9GIfnH0+zRHaRi2Zja6gKCWfLrTUKiVRVSDKQ5u14AK9WhLqVEqsv5FBzYirnyqkDSODnazlxQUe260QMl1OsmGAI0K0cl8E7ujBUgXQRPYsgtrzuA6ya1Ain58w4MGRrL8viMr8eZfQIDAQAB
-----END PUBLIC KEY-----';

// 将 XML 数据转换为 UTF-8 编码
$xmlData_utf8 = mb_convert_encoding($xmlData, 'UTF-8');
var_dump($xmlData_utf8);
// 使用 RSA 公钥加密数据
$result = openssl_public_encrypt($xmlData, $encryptedData, $rsaPublicKey);
var_dump($result);
// 检查加密是否成功
if ($result === false) {
    echo 'Failed to encrypt data';
} else {
    // 对加密后的数据进行 BASE64 编码
    $encryptedData_base64 = base64_encode($encryptedData);
    echo 'Encrypted data: ' . $encryptedData_base64;
}
